package edu.xidian.bdilab.jrbigdata.service.enterprise.changepassword.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseUser;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseUserMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.changepassword.changepasswordService;
import edu.xidian.bdilab.jrbigdata.util.DecryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class changepasswordServiceimpl implements changepasswordService {
    @Autowired
    EnterpriseUserMapper enterpriseuserMapper;

    @Override
    public ResponseResult changeWord(String username, String password, String newPassword) {
        EnterpriseUser person = enterpriseuserMapper.selectById(username);
        ResponseResult responseResult = new ResponseResult();
        String md5Password = DecryptUtils.getMd5(password);
        if (person == null) {
            //no person
            responseResult.setCode(EnterpriseCode.CHANGEPASSWORD_FAILED.getCode());
            responseResult.setMessage(EnterpriseCode.CHANGEPASSWORD_SUCCESS.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }

        else if(!(md5Password.equals(person.getPassword()))) {
            //wrong password
            responseResult.setMessage(EnterpriseCode.CHANGEPASSWORD_FAILED.getMessage());
            responseResult.setCode(EnterpriseCode.CHANGEPASSWORD_FAILED.getCode());
            responseResult.setSuccess(false);
            return responseResult;
        }
        String md5NewPassword = DecryptUtils.getMd5(newPassword);
        person.setPassword(md5NewPassword);
        person.setUserName(username);
        enterpriseuserMapper.updateById(person);

        responseResult.setMessage(EnterpriseCode.CHANGEPASSWORD_SUCCESS.getMessage());
        responseResult.setCode(EnterpriseCode.CHANGEPASSWORD_SUCCESS.getCode());
        responseResult.setSuccess(true);
        return responseResult;
    }
}
